package com.ruoyi.recyclecomprehensive.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.recycle.domain.*;
import com.ruoyi.recycle.dto.EssayQuestionDTO;
import com.ruoyi.recycle.dto.TrainVideoDTO;
import com.ruoyi.recycle.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home")
@Api(tags = "首页")
public class HomePageController extends BaseController {
    @Autowired
    private ITrainVideoService trainVideoService;
    @Autowired
    private IHomePageSlideshowService homePageSlideshowService;
    @Autowired
    private IEssayQuestionService essayQuestionService;
    @Autowired
    private CompanyVideoService videoService;
    @Autowired
    private RankingListService rankingListService;
    @Autowired
    private IFeedbackService feedbackService;
    @Autowired
    private CompanyPictureService companyPictureService;
    @Autowired
    private CompanyIntroductionService introductionService;
    @Autowired
    private IConsultManagementService consultManagementService;
    @Autowired
    private IPlatformInformManagementService platformInformManagementService;


    /**
     * 分页查询培训视频列表
     */
    @PostMapping("/videoPageQuery")
    @ApiOperation("分页查询培训视频列表")
    public R<IPage<TrainVideo>> videoPageQuery(@RequestBody TrainVideoDTO queryDTO) {
        final TrainVideo factory = new TrainVideo();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<TrainVideo> wrapper = Wrappers.lambdaQuery(factory);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(TrainVideo::getCreatedTime, queryDTO.getSearchTimeBegin());
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(TrainVideo::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        return R.ok(trainVideoService.page(new Page<TrainVideo>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper));
    }


    @GetMapping("/queryCompanyVideo")
    @ApiOperation("查询公司视频列表")
    public R<List<CompanyVideo>> queryCompanyVideo() {
        return R.ok(videoService.list());
    }

    @GetMapping("/queryRankingList")
    @ApiOperation("查询排行榜列表")
    public R<RankingList> queryRankingList() {
        return R.ok(CollUtil.isNotEmpty(rankingListService.list()) ? rankingListService.list().get(0) : null);
    }


    /**
     * 分页首页轮播广告条件查询
     */
    @PostMapping("/slideshow")
    @ApiOperation("首页轮播广告")
    public R<List<HomePageSlideshow>> slideshow() {
        return R.ok(homePageSlideshowService.list(Wrappers.lambdaQuery(new HomePageSlideshow()).eq(HomePageSlideshow::getType, "0")));
    }


    /**
     * 分页查询问答列表
     */
    @PostMapping("/essayPageQuery")
    @ApiOperation("分页查询问答列表")
    public IPage<EssayQuestion> essayPageQuery(@RequestBody EssayQuestionDTO queryDTO) {
        final EssayQuestion factory = new EssayQuestion();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<EssayQuestion> wrapper = Wrappers.lambdaQuery(factory);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(EssayQuestion::getCreatedTime, queryDTO.getSearchTimeBegin());
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(EssayQuestion::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        return essayQuestionService.page(new Page<EssayQuestion>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    @GetMapping("/searchEssay")
    @ApiOperation("搜索问题")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "问题类型"), @ApiImplicitParam(name = "search", value = "搜索文字")})
    public R<List<EssayQuestion>> searchEssay(@RequestParam String type, @RequestParam String search) {
        final LambdaQueryWrapper<EssayQuestion> queryWrapper = Wrappers.lambdaQuery(new EssayQuestion())
                .eq(EssayQuestion::getType, type)
                .like(EssayQuestion::getQuestion, "%" + search + "%");
        return R.ok(essayQuestionService.list(queryWrapper));
    }

    @ApiOperation("获取公告列表")
    @GetMapping("/announcementList")
    public R<List<PlatformInformManagement>> announcementList() {
        return R.ok(platformInformManagementService.list());
    }

    @PostMapping("/saveFeedback")
    @ApiOperation("提交反馈")
    public R<Boolean> saveFeedback(@RequestBody Feedback feedback) {
        return R.ok(feedbackService.save(feedback));
    }

    @ApiOperation("获取公司图片")
    @GetMapping("/getCompanyPicture")
    public R<CompanyPicture> getCompanyPicture() {
        return R.ok(companyPictureService.getById(1L));

    }

    @ApiOperation("获取公司简介")
    @GetMapping("/getCompanyIntroduction")
    public R<CompanyIntroduction> getCompanyIntroduction() {
        return R.ok(introductionService.getById(1L));

    }

    @ApiOperation("获取咨询")
    @GetMapping("/getInformationReport")
    public R<List<ConsultManagement>> getInformationReport() {
        return R.ok(consultManagementService.list());
    }


}
